package com.ignou.mca.login;

import java.io.IOException;

import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.ignou.mca.datasource.DatabaseOperations;

/**
 * Servlet implementation class ChangePassword
 */
public class ChangePassword extends HttpServlet 
{
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public ChangePassword() 
    {
        super();
        // TODO Auto-generated constructor stub
    }


	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException 
	{
		String oldPassword = request.getParameter("oldPassword");
		String newPassword = request.getParameter("newPassword");
		String userName = request.getParameter("userName");
		HttpSession session = request.getSession();
		DatabaseOperations dbOps = new DatabaseOperations();
		Boolean isChanged = dbOps.changePassword(userName, oldPassword, newPassword);
		ServletContext ctx = getServletContext();
		
		if(isChanged)
		{
			response.sendRedirect("./Logout.jsp");
		}else
		{
			session.setAttribute("ErrorMessage", "Change password is unsuccessful");
			ctx.getRequestDispatcher("/ErrorDisplay.jsp").forward(request, response);
		}
	}
}
